package org.jeecg.modules.demo.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.mapping.Map;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.member.entity.JkMemberInfo;
import org.jeecg.modules.demo.member.service.IJkMemberInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.util.RandomStringGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: jk_member_info
 * @Author: jeecg-boot
 * @Date:   2025-05-18
 * @Version: V1.0
 */
@Tag(name="jk_member_info")
@RestController
@RequestMapping("/member/jkMemberInfo")
@Slf4j
public class JkMemberInfoController extends JeecgController<JkMemberInfo, IJkMemberInfoService> {
	@Autowired
	private IJkMemberInfoService jkMemberInfoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param jkMemberInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "jk_member_info-分页列表查询")
	@Operation(summary="jk_member_info-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<JkMemberInfo>> queryPageList(JkMemberInfo jkMemberInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<JkMemberInfo> queryWrapper = QueryGenerator.initQueryWrapper(jkMemberInfo, req.getParameterMap());
		Page<JkMemberInfo> page = new Page<JkMemberInfo>(pageNo, pageSize);
		IPage<JkMemberInfo> pageList = jkMemberInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param jkMemberInfo
	 * @return
	 */
	@AutoLog(value = "jk_member_info-添加")
	@Operation(summary="jk_member_info-添加")
	@RequiresPermissions("member:jk_member_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JkMemberInfo jkMemberInfo) {
		jkMemberInfoService.save(jkMemberInfo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jkMemberInfo
	 * @return
	 */
	@AutoLog(value = "jk_member_info-编辑")
	@Operation(summary="jk_member_info-编辑")
	@RequiresPermissions("member:jk_member_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JkMemberInfo jkMemberInfo) {
		jkMemberInfoService.updateById(jkMemberInfo);
		return Result.OK("编辑成功!");
	}


	 @AutoLog(value = "jk_member_info-重置密码")
	 @Operation(summary="jk_member_info-重置密码")
	 @RequiresPermissions("member:jk_member_info:edit")
	 @RequestMapping(value = "/restPass", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<HashMap> restPass(@RequestParam(name="id",required=true) String id, @RequestParam(name="userName",required=true) String userName) {

		 JkMemberInfo memberInfo = new JkMemberInfo();
		 memberInfo.setId(Integer.parseInt(id));
		 //String username = memberInfo.getUsername();
		 String password = RandomStringGenerator.generateRandomString(6);

		 String salt = oConvertUtils.randomGen(8);
		 String passwordEncode = PasswordUtil.encrypt(userName, password, salt);
		 memberInfo.setPassword(passwordEncode); // 注意：实际应用中需先加密密码
		 memberInfo.setSalt(salt);
		 jkMemberInfoService.updateById(memberInfo);
		 HashMap<String,String> map = new HashMap<String,String>();
		 map.put("password",password);
		 return Result.OK(map);
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "jk_member_info-通过id删除")
	@Operation(summary="jk_member_info-通过id删除")
	@RequiresPermissions("member:jk_member_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jkMemberInfoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "jk_member_info-批量删除")
	@Operation(summary="jk_member_info-批量删除")
	@RequiresPermissions("member:jk_member_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jkMemberInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "jk_member_info-通过id查询")
	@Operation(summary="jk_member_info-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JkMemberInfo> queryById(@RequestParam(name="id",required=true) String id) {
		JkMemberInfo jkMemberInfo = jkMemberInfoService.getById(id);
		if(jkMemberInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jkMemberInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jkMemberInfo
    */
    @RequiresPermissions("member:jk_member_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JkMemberInfo jkMemberInfo) {
        return super.exportXls(request, jkMemberInfo, JkMemberInfo.class, "jk_member_info");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("member:jk_member_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JkMemberInfo.class);
    }

}
